<?php


namespace app\services\delivery;


use app\models\delivery\DeliveryOrderRecordModel;
use app\services\BaseService;

class DeliveryOrderRecordService extends BaseService {

    protected static function initModel() {
        return new DeliveryOrderRecordModel();
    }

    /**
     * 获取订单的出入库记录
     * @param string $orderNo
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecordByOrderNo(string $orderNo): array {
        $record = self::initModel()->field('goods_name,put_in_num,take_out_num')->where('order_no', $orderNo)->select()->toArray();
        return $record;
    }

    /**
     * 保存数据
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public static function create($data) {
        $res = self::initModel()->saveAll($data);
        return $res;
    }

    /**
     * 获取某字段总和
     * @param $orderNo
     * @param $goodsName
     * @param $field
     * @return float
     */
    public static function getSumNum($orderNo, $goodsName, $field) {
        $num = self::initModel()
            ->where('order_no', $orderNo)
            ->where('goods_name', $goodsName)
            ->sum($field);
        return $num;
    }
}